package com.example.helloandroid;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 1、加载xml声明的布局（显示视图， 参数为资源文件名字）
       // setContentView(R.layout.activity_main);
        /* 创建一个线性布局管理器
         *无论创建哪种UI 都要传入这个Context参数,代表访问Android应用环境的全局信息的API
         */
//        LinearLayout layout = new LinearLayout(this);
//        super.setContentView(layout);
//        layout.setOrientation(LinearLayout.VERTICAL);
//        TextView t1 = new TextView(this);
//        t1.setText("Hello Android!");
//        layout.addView(t1);

        //2、方式二加载布局
        View view = getView();
        setContentView(view);
    }

    private View getView(){
        LinearLayout layout = new LinearLayout(this);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(layoutParams);
        layout.setOrientation(LinearLayout.VERTICAL);
        TextView t1 = new TextView(this);
        t1.setText("Hello Android for view!");
        FrameLayout.LayoutParams textViewLayoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        layout.addView(t1, textViewLayoutParams);
        return layout;
    }
}